package Convert_stream_package;

import java.io.*;
import java.nio.charset.Charset;

public class Convert_stream_demo3 {
    public static void main(String[] args) throws IOException {
        /*
            将本地文件中的GBK文件，转成UTF-8
         */

        //1、JDK11以前的方案
        InputStreamReader isr = new InputStreamReader(new FileInputStream("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Convert_stream_module/src/rough_draft/a.txt"), "GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Convert_stream_module/src/rough_draft/b.txt"), "UTF-8");

        int b;
        while ((b = isr.read()) != -1) {
            osw.write(b);
        }

        osw.close();
        isr.close();

        //2、替代方案
        FileReader fr = new FileReader("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Convert_stream_module/src/rough_draft/a.txt", Charset.forName("GBK"));
        FileWriter fw = new FileWriter("/Users/zhaoxiaoyong/Documents/Java/" +
                "IO_stream_project/Convert_stream_module/src/rough_draft/b.txt", Charset.forName("UTF-8"));

        while ((b = fr.read()) != -1) {
            fw.write(b);
        }
        fw.close();
        fr.close();

    }
}
